package com.dragon.user_service.manager.sharding;

import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;

import java.util.Collection;
import java.util.Properties;

public class UserShardingAlgorithm implements StandardShardingAlgorithm<Long> {
    
    @Override
    public String doSharding(Collection<String> tableNames, PreciseShardingValue<Long> shardingValue) {
        // 计算分片：user_id % 5
        long tableIndex = shardingValue.getValue() % 5;
        String targetTable = "user_" + tableIndex;
        
        // 检查表名是否存在
        if (tableNames.contains(targetTable)) {
            return targetTable;
        }
        throw new IllegalArgumentException("分表路由失败，表不存在: " + targetTable);
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        return null;
    }

    @Override
    public String getType() {
        return "USER_TABLE_SHARDING"; // 自定义类型标识
    }

    @Override
    public Properties getProps() {
        return null;
    }

    @Override
    public void init(Properties properties) {

    }
}